package py.com.fms.session;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

//import javax.faces.component.UIComponent;
//import javax.faces.context.FacesContext;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage.Severity;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import py.com.fms.exception.CommonException;
import py.com.fms.session.ServiceSqlMap;
import py.com.fms.ven.bean.CustomerBean;
import py.com.fms.ven.persistence.services.CustomerServices;

// Project classes imports
import py.com.fms.fms.persistence.services.*;
import py.com.fms.gnd.persistence.services.*;
import py.com.fms.com.persistence.services.*;
import py.com.fms.prd.persistence.services.*;

// Beans
import py.com.fms.fms.bean.*;
import py.com.fms.gnd.bean.*;
import py.com.fms.com.bean.*;
import py.com.fms.prd.bean.*;

// Maintenances
import py.com.fms.fms.session.maintenances.*;

@SuppressWarnings("unused")
@Name("withinHolder")
@Scope(ScopeType.APPLICATION)
public class WithinHolder {
	
	@In("#{serviceSqlMap}")
	private ServiceSqlMap sqlMap;
	@Logger
	private Log log;
		
	// Collection Attributes
	// currencies
	private static Collection<CurrencyBean> allCurrencies;
	
	// countries
	private Collection<CountryBean> allCountries;
	
	// persons
	private Collection<PersonBean> allPerson;
	
	// enterprises
	private Collection<EnterpriseBean> allEnterprise;
	
	// customers
	private Collection<CustomerBean> allCustomers;
	
	// employees
	private Collection<EmployeeBean> allEmp;
	
	// branch
	private Collection<BranchBean> allBranch;
	
	
	// references
	private Collection<RefHeadBean> allRefHead;
	private Collection<RefHeadBean> refHeadList;
	private Collection<RefDetBean> allRefDet;
	private Collection<RefDetBean> refDetList;
	
	// users
	private Collection<UsersBean> allUsers;
	
	// roles
	private Collection<RolesBean> allRoles;

	// enfermedades
	private Collection<AfflictionBean> allAfflictions;
	
	// sintomas
	private Collection<SymptomBean> allSymptoms;
	
	// tratamientos
	private Collection<TreatmentBean> allTreatments;
	
	// Cuentas bancarias
	private static Collection<BankAccountsBean> allBankAccounts;
	
	// FMS MODULE
	private static Collection<MeasureUnitBean> allMeasureUnit;
	private static Collection<IvaBean> allTaxes;
	private static Collection<VoucherBean> allVouchers;
	private static Collection<VoucherBean> voucherListPurchases = new ArrayList<VoucherBean>();
	private static Collection<VoucherBean> voucherList = new ArrayList<VoucherBean>();
	
	// COM MODULE
	private static Collection<BuyConditionBean> allBuyCondition;
	private static Collection<SupplierBean> allSupplier;
	private static Collection<OrderHeadBean> allOrderHead;
	private static Collection<OrderHeadBean> supplierOrders;
	private static Collection<OrderDetBean> allOrderDet;
	private static Collection<OrderDetBean> orderDetByHead;
	private static Collection<PurchasesHeadBean> allPurchasesHead;
	private static Collection<PaymentOrderDetBean> allPaymentOrderDet;
	private static Collection<PaymentOrderDetBean> paymentOrderDetForPay;
	
	
	// PRD MODULE
	private static Collection<SuppliesBean> allSupplies;
	
	private static Collection<BurdenBean> allBurden;
	
	private static Collection<GroupBean> allGroup;
	
	private static Collection<FamilyBean> allFamily;
	
	private static Collection<SuppliesTypeBean> allSuppliesType;
	

	
	// METHODS
	public void initialize(){
		allCountries = null;
		allCurrencies = null;
		allEnterprise = null;
		allPerson = null;
		allRefDet = null;
		refDetList = null;
		allCustomers = null;
		allEmp = null;
		allBranch = null;
		allUsers = null;
		allRoles = null;
		allBurden = null;
		allGroup = null;
		allFamily = null;
		allSuppliesType = null;
		allOrderHead = null;
		supplierOrders = null;
		allOrderDet= null;
		allBankAccounts = null;
	}
	
	public Collection<CurrencyBean> currencyList() {
		try {
			if (allCurrencies == null){
				CurrencyServices crSrv = new CurrencyServices(this.sqlMap.getConfig());
				allCurrencies = crSrv.getAllCurrencies();
			}

		} catch (CommonException e) {
			this.log.error(e);
		}
		return allCurrencies;
	}
	

	public CurrencyBean currencyData(String descrip) {
		if (descrip != null) {
			this.currencyList();
			for (CurrencyBean c : allCurrencies) {
				if (descrip.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<CountryBean> countryList() {
		try {
			if (this.allCountries == null){
				CountryServices cnSrv = new CountryServices(this.sqlMap.getConfig());
				this.allCountries = cnSrv.getAllCountry();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allCountries;
	}
	

	public CountryBean countryData(String descrip) {
		if (descrip != null) {
			this.countryList();
			for (CountryBean c : this.allCountries) {
				if (descrip.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<PersonBean> personList() {
		try {
			this.initialize();
			if (this.allPerson == null){
				PersonServices prSrv = new PersonServices(this.sqlMap.getConfig());
				this.allPerson = prSrv.getAllPersons();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allPerson;
	}
	

	public PersonBean personData(String cod) {
		if (cod != null) {
			this.personList();
			for (PersonBean pr : this.allPerson) {
				if (cod.equals(pr.getCod())){
					return pr;
				}
			}
		}
		return null;
	}
	
	public Collection<EnterpriseBean> entList() {
		try {
			this.initialize();
			if (this.allEnterprise == null){
				EnterpriseServices enSrv = new EnterpriseServices(this.sqlMap.getConfig());
				this.allEnterprise = enSrv.getAllEnterprises();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allEnterprise;
	}
	

	public EnterpriseBean entData(String name) {
		if (name != null) {
			this.entList();
			for (EnterpriseBean c : this.allEnterprise) {
				if (name.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<RefHeadBean> refHeadList() {
		try {
			this.initialize();
			if (this.allRefHead == null){
				RefServices rnSrv = new RefServices(this.sqlMap.getConfig());
				this.allRefHead = rnSrv.getAllRefHead();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allRefHead;
	}
	
	public RefHeadBean refHeadData(String cod) {
		if (cod != null) {
			this.refHeadList();
			for (RefHeadBean c : this.allRefHead) {
				if ((cod).equals(c.getCod())) {
					return c;
				}
			}
		}else{
			System.out.println("Null Descrip (Within H.)");
		}
		return null;
	}
	
	public Collection<RefDetBean> refDetList() {
		try {
			this.initialize();
			if (this.allRefDet == null){
				RefServices rnSrv = new RefServices(this.sqlMap.getConfig());
				this.allRefDet = rnSrv.getAllRefDet();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allRefDet;
	}
		

	public RefDetBean refDetData(String id) {
		if (id != null) {
			this.refDetList();
			for (RefDetBean c : this.allRefDet) {
				if ((id).equals(c.getId().toString())) {
					return c;
				}
			}
		}else{
			System.out.println("Null Descrip (Within H.)");
		}
		return null;
	}
	
	public Collection<CustomerBean> customerList() {
		try {
			this.initialize();
			if (this.allCustomers == null){
				CustomerServices clSrv = new CustomerServices(this.sqlMap.getConfig());
				this.allCustomers = clSrv.getAllCustomers();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allCustomers;
	}
	

	public CustomerBean customerData(String arg) {
		if (arg != null) {
			this.customerList();
			for (CustomerBean c : this.allCustomers) {
				if (arg.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<EmployeeBean> employeeList() {
		try {
			this.initialize();
			if (this.allEmp == null){
				EmployeeServices emSrv = new EmployeeServices(this.sqlMap.getConfig());
				this.allEmp = emSrv.getAllEmployees();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allEmp;
	}
	

	public EmployeeBean employeeData(String arg) {
		if (arg != null) {
			this.employeeList();
			for (EmployeeBean c : this.allEmp) {
				if (arg.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<BranchBean> branchList() {
		try {
			this.initialize();
			if (this.allBranch == null){
				BranchServices brSrv = new BranchServices(this.sqlMap.getConfig());
				this.allBranch = brSrv.getAllBranches();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allBranch;
	}
	

	public BranchBean branchData(String arg) {
		if (arg != null) {
			this.branchList();
			for (BranchBean c : this.allBranch) {
				if (arg.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
		
	
	
	public Collection<UsersBean> userList() {
		try {
			this.initialize();
			if (this.allUsers == null){
				UsersServices usSrv = new UsersServices(this.sqlMap.getConfig());
				this.allUsers = usSrv.getAllUsers();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allUsers;
	}
	

	public UsersBean userData(String arg) {
		if (arg != null) {
			this.userList();
			for (UsersBean c : this.allUsers) {
				if (arg.equals(c.getUserName())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<RolesBean> roleList() {
		try {
			this.initialize();
			if (this.allRoles == null){
				RolesServices rlSrv = new RolesServices(this.sqlMap.getConfig());
				this.allRoles = rlSrv.getAllRoles();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allRoles;
	}
	

	public RolesBean roleData(String arg) {
		if (arg != null) {
			this.roleList();
			for (RolesBean c : this.allRoles) {
				if (arg.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
	


	/**
	 * copiado de withinHelper
	 */
	public Collection<AfflictionBean> afflictionList() {
		try {
			this.initialize();
			if (this.allAfflictions == null){
				AfflictionServices admSrv = new AfflictionServices(this.sqlMap.getConfig());
				this.allAfflictions = admSrv.getAllAfflictions();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allAfflictions;
	}public WithinHolder() {
		// TODO Auto-generated constructor stub
	}
	
	public AfflictionBean afflictionData(String descrip) {
		if (descrip != null) {
			this.afflictionList();
			for (AfflictionBean c : this.allAfflictions) {
				if (descrip.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<SymptomBean> symptomList() {
		try {
			this.initialize();
			if (this.allSymptoms == null){
				SymptomServices admSrv = new SymptomServices(this.sqlMap.getConfig());
				this.allSymptoms = admSrv.getAllSymptoms();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allSymptoms;
	}
	

	public SymptomBean symptomData(String descrip) {
		if (descrip != null) {
			this.symptomList();
			for (SymptomBean c : this.allSymptoms) {
				if (descrip.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<TreatmentBean> treatmList() {
		try {
			this.initialize();
			if (this.allTreatments == null){
				TreatmentServices admSrv = new TreatmentServices(this.sqlMap.getConfig());
				this.allTreatments = admSrv.getAllTreatments();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.allTreatments;
	}
	

	public TreatmentBean treatmData(String descrip) {
		if (descrip != null) {
			this.symptomList();
			for (TreatmentBean c : this.allTreatments) {
				if (descrip.equals(c.getDescrip())) {
					return c;
				}
			}
		}
		return null;
	}
	
	// FMS Module --------------------------------------
	
	public Collection<MeasureUnitBean> measureUnitList() {
		try {
			if (allMeasureUnit == null){
				MeasureUnitServices muSrv = new MeasureUnitServices(this.sqlMap.getConfig());
				allMeasureUnit = muSrv.getAllMeasureUnit();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allMeasureUnit;
	}
	
	public MeasureUnitBean measureUnitData(String elem) {
		if (elem != null) {
			this.measureUnitList();
			for (MeasureUnitBean c : allMeasureUnit) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<IvaBean> taxesList() {
		try {
			if (allTaxes == null){
				IvaServices muSrv = new IvaServices(this.sqlMap.getConfig());
				allTaxes = muSrv.getAllIva();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allTaxes;
	}
	
	public IvaBean taxesData(String elem) {
		if (elem != null) {
			this.measureUnitList();
			for (IvaBean c : allTaxes) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<RefDetBean> refDetListByHeadCod(String headCode) {
		this.refDetList = null;
		try {
			if (this.refDetList == null){
				RefServices rnSrv = new RefServices(this.sqlMap.getConfig());
				this.refDetList = rnSrv.getRefDetByHeadCode(headCode);
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return this.refDetList;
	}
	
	public Collection<BankAccountsBean> bankAccountsList() {
		try {
			if (allBankAccounts == null){
				BankAccountsServices baSrv = new BankAccountsServices(this.sqlMap.getConfig());
				allBankAccounts = baSrv.getAllBankAccount();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allBankAccounts;
	}
	
	public BankAccountsBean bankAccountsData(String elem) {
		if (elem != null) {
			this.supplierList();
			for (BankAccountsBean c : allBankAccounts) {
				if (elem.equals(c.getId().toString())) {
					return c;
				}
			}
		}
		return null;
	}
	
	// COM Module --------------------------------------
	
	public Collection<BuyConditionBean> buyConditionList() {
		try {
			if (allBuyCondition == null){
				BuyConditionServices bcSrv = new BuyConditionServices(this.sqlMap.getConfig());
				allBuyCondition = bcSrv.getAllBuyCondition();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allBuyCondition;
	}
	
	public BuyConditionBean buyConditionData(String elem) {
		if (elem != null) {
			this.measureUnitList();
			for (BuyConditionBean c : allBuyCondition) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<SupplierBean> supplierList() {
		try {
			if (allSupplier == null){
				SupplierServices spSrv = new SupplierServices(this.sqlMap.getConfig());
				allSupplier = spSrv.getAllSupplier();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allSupplier;
	}
	
	public SupplierBean supplierData(String elem) {
		if (elem != null) {
			this.supplierList();
			for (SupplierBean c : allSupplier) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<OrderHeadBean> orderHeadList() {
		try {
			this.initialize();
			if (allOrderHead == null){
				OrderServices orSrv = new OrderServices(this.sqlMap.getConfig());
				allOrderHead = orSrv.getAllOrderHead();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allOrderHead;
	}
	
	public Collection<OrderHeadBean> supplierOrdersList(Long supplierId) {
		try {
			this.initialize();
			if (supplierOrders == null){
				OrderServices orSrv = new OrderServices(this.sqlMap.getConfig());
				supplierOrders = orSrv.getOrderBySupplierId(supplierId);
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return supplierOrders;
	}
	

	public OrderHeadBean orderHeadData(String arg) {
		if (arg != null) {
			this.orderHeadList();
			for (OrderHeadBean bean : allOrderHead) {
				if (arg.equals(bean.getId().toString())) {
					System.out.println("BEAN ORDER -----------: " + bean);
					return bean;
				}
			}
		}
		return null;
	}
	
	public Collection<OrderDetBean> orderDetList() {
		try {
			this.initialize();
			if (allOrderDet == null){
				OrderServices orSrv = new OrderServices(this.sqlMap.getConfig());
				allOrderDet = orSrv.getAllOrderDet();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allOrderDet;
	}
	
	public Collection<OrderDetBean> orderDetByHead(Long headId){
		try {
			if (orderDetByHead == null){
				OrderServices orSrv = new OrderServices(this.sqlMap.getConfig());
				orderDetByHead = orSrv.getOrderDetByHeadId(headId);
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return orderDetByHead;
	}
	

	public OrderDetBean orderDetData(String arg) {
		if (arg != null) {
			this.orderHeadList();
			for (OrderDetBean bean : allOrderDet) {
				if (arg.equals(bean.getId().toString())) {
					return bean;
				}
			}
		}
		return null;
	}
	
	public Collection<VoucherBean> voucherList() {
		try {
			if (allVouchers == null){
				VoucherServices vcSrv = new VoucherServices(this.sqlMap.getConfig());
				allVouchers = vcSrv.getAllVoucher();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allVouchers;
	}
	
	
	
	public VoucherBean voucherData(String elem) {
		if (elem != null) {
			this.voucherList();
			for (VoucherBean c : allVouchers) {
				if (elem.equals(c.getAbrev())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public Collection<VoucherBean> voucherListTransaction(String transactionType) {
		try {
			System.out.println("antes del this.voucherList().. size = " + voucherList.size());
			this.voucherList();
			if (voucherList.isEmpty()) {
				System.out.println("dentro de voucherList.isEmpty(). allVoucher= " + allVouchers.size());
				Iterator<VoucherBean> it = allVouchers.iterator();
				if(transactionType.equals("SALE")){
					while (it.hasNext()) {
						System.out.println("dentro del while");
						VoucherBean next = it.next();
						if (next.getViewAtPurchase()) {
							System.out.println("*** encontramos valor voucher = S ****");
							voucherList.add(next);
						}
					}
				}
				if(transactionType.equals("PURCHASE")){
					while (it.hasNext()) {
						System.out.println("dentro del while");
						VoucherBean next = it.next();
						if (next.getViewAtSale()) {
							System.out.println("*** encontramos valor voucher = S ****");
							voucherList.add(next);
						}
					}
				}
			}
		} catch (Exception e) {
			System.out.println("**** error en voucherListPurchases ****");
			this.log.error(e);
		}
		return voucherListPurchases;
	}
	
	public Collection<VoucherBean> voucherListPurchases() {
		try {
			System.out.println("antes del this.voucherList().. size = " + voucherListPurchases.size());
			this.voucherList();
			if (voucherListPurchases.isEmpty()) {
				System.out.println("dentro de voucherListPurchases.isEmpty(). allVoucher= " + allVouchers.size());
				Iterator<VoucherBean> it = allVouchers.iterator();
				while (it.hasNext()) {
					System.out.println("dentro del while");
					VoucherBean next = it.next();
					if (next.getViewAtPurchase()) {
						System.out.println("*** encontramos valor voucher = S ****");
						voucherListPurchases.add(next);
					}
				}
			}
		} catch (Exception e) {
			System.out.println("**** error en voucherListPurchases ****");
			this.log.error(e);
		}
		return voucherListPurchases;
	}
	
	public Collection<VoucherBean> voucherListSale() {
		try {
			if (allVouchers == null){
				VoucherServices vcSrv = new VoucherServices(this.sqlMap.getConfig());
				allVouchers = vcSrv.getAllVoucherSale();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allVouchers;
	}
	
	public Collection<VoucherBean> voucherListReceive() {
		try {
			if (allVouchers == null){
				VoucherServices vcSrv = new VoucherServices(this.sqlMap.getConfig());
				allVouchers = vcSrv.getAllVoucherReceive();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allVouchers;
	}
	
	public Collection<PurchasesHeadBean> purchasesHeadList() {
		try {
			this.initialize();
			if (allPurchasesHead == null){
				PurchasesServices prSrv = new PurchasesServices(this.sqlMap.getConfig());
				allPurchasesHead = prSrv.getAllPurchaseHead();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allPurchasesHead;
	}
	
	public Collection<PaymentOrderDetBean> paymentOrderDetList() {
		try {
			this.initialize();
			if (allPaymentOrderDet == null){
				PaymentOrderServices podSrv = new PaymentOrderServices(this.sqlMap.getConfig());
				allPaymentOrderDet = podSrv.getAllPaymentOrderDet();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allPaymentOrderDet;
	}
	
	public Collection<PaymentOrderDetBean> getFeesToSuppliersForPaymentList(Long idEnt, Long idSupplier, Date startDate, Date endDate){
		/* utilizamos el services de compras para generar el bean de orden de pago a partir
		 * del una consulta a las tablas COM_COMPRA_CAB y COM_SALDOS_PROVEEDORES.
		 * */
		try {
			this.initialize();
			if (paymentOrderDetForPay == null){
				PurchasesServices prSrv = new PurchasesServices(this.sqlMap.getConfig());
				System.out.println("antes de llamar al services-xml");
				paymentOrderDetForPay = prSrv.getFeesToSuppliersForPayment(idEnt, idSupplier, startDate, endDate);
				System.out.println("despues de llamar al services-xml");
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return paymentOrderDetForPay;
	}
	
	public PurchasesHeadBean purchasesHeadData(String arg) {
		if (arg != null) {
			this.purchasesHeadList();
			for (PurchasesHeadBean bean : allPurchasesHead) {
				if (arg.equals(bean.getId().toString())) {
					System.out.println("BEAN ORDER -----------: " + bean);
					return bean;
				}
			}
		}
		return null;
	}
	
	public PaymentOrderDetBean paymentOrderDetData(String arg) {
		if (arg != null) {
			this.paymentOrderDetList();
			for (PaymentOrderDetBean bean : allPaymentOrderDet) {
				if (arg.equals(bean.getId().toString())) {
					System.out.println("BEAN ORDER -----------: " + bean);
					return bean;
				}
			}
		}
		return null;
	}
	
	
	// PRD Module --------------------------------------
	
	public Collection<SuppliesBean> suppliesList() {
		try {
			if (allSupplies == null){
				SuppliesServices bcSrv = new SuppliesServices(this.sqlMap.getConfig());
				allSupplies = bcSrv.getAllSupplies();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allSupplies;
	}
	
	public SuppliesBean suppliesData(String elem) {
		if (elem != null) {
			this.supplierList();
			for (SuppliesBean c : allSupplies) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	public List<String> suppliesSuggest(Object suggest) {
		try {
			String pref = (String) suggest;
			ArrayList<String> result = new ArrayList<String>();
			if (allSupplies == null){
				SuppliesServices bcSrv = new SuppliesServices(this.sqlMap.getConfig());
				allSupplies = bcSrv.getAllSupplies();
			}
			
			Iterator<SuppliesBean> iterator = allSupplies.iterator();
			while (iterator.hasNext()) {
				SuppliesBean next = iterator.next();
				String elem = ((String) next.getDescrip());
				if ((elem != null && elem.toLowerCase().indexOf(pref.toLowerCase()) == 0)
						|| "".equals(pref)) {
					result.add(elem);
				}
			}
			return result;
		} catch (CommonException e) {
			this.log.error(e);
		}
		return null;
 
	}

	
	
	
    // PARA RUBRO GRUPO Y FAMILIA	
	
	public Collection<BurdenBean> burdenList() {
		try {
			if (allBurden == null){
				BurdenServices bcSrv = new BurdenServices(this.sqlMap.getConfig());
				allBurden = bcSrv.getAllBurden();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allBurden;
	}
	
	public BurdenBean burdenData(String elem) {
		if (elem != null) {
			this.burdenList();
			for (BurdenBean c : allBurden) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	
	public Collection<GroupBean> groupList() {
		try {
			if (allGroup == null){
				GroupServices bcSrv = new GroupServices(this.sqlMap.getConfig());
				allGroup = bcSrv.getAllGroup();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allGroup;
	}
	
	public GroupBean groupData(String elem) {
		if (elem != null) {
			this.groupList();
			for (GroupBean c : allGroup) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	
	public Collection<FamilyBean> familyList() {
		try {
			if (allFamily == null){
				FamilyServices bcSrv = new FamilyServices(this.sqlMap.getConfig());
				allFamily = bcSrv.getAllFamily();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allFamily;
	}
	
	public FamilyBean familyData(String elem) {
		if (elem != null) {
			this.familyList();
			for (FamilyBean c : allFamily) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	
	
	// tipo de insumo
	
	public Collection<SuppliesTypeBean> suppliesTypeList() {
		try {
			if (allSuppliesType == null){
				SuppliesTypeServices bcSrv = new SuppliesTypeServices(this.sqlMap.getConfig());
				allSuppliesType = bcSrv.getAllSuppliesType();
			}
		} catch (CommonException e) {
			this.log.error(e);
		}
		return allSuppliesType;
	}
	
	public SuppliesTypeBean suppliesTypeData(String elem) {
		if (elem != null) {
			this.suppliesTypeList();
			for (SuppliesTypeBean c : allSuppliesType) {
				if (elem.equals(c.getCod())) {
					return c;
				}
			}
		}
		return null;
	}
	
	
	

	
	
	// Cleaning Mutators
	public static void cleanAllCurrencies(){
		allCurrencies  = null;
	}
	
	public void cleanAllCountries(){
		this.allCountries  = null;
	}

}
